﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreConverters;
using VDS.RDF.Query;
using VDS.RDF.Storage;
using CoreBase;
using VDS.RDF;
using VDS.RDF.Parsing;
using VDS.RDF.Translation;
using Triple = VDS.RDF.Triple;

namespace CoreVirtuoso
{
    public class VirtuosoStore
    {
        public StorageSettings StorageSettings { get; set; }

        private VirtuosoManager VirtuosoManager { get; set; }

        public VirtuosoStore(StorageSettings storageSettings)
        {
            this.StorageSettings = storageSettings;

            this.VirtuosoManager = new VirtuosoManager("79.136.196.8", 1111, "DB", "dba", "dba");
        }

        //public void UploadFileToGraph(string fileName, string graphName)
        //{
        //    Graph graph = new Graph();
        //    RdfXmlParser rdfXmlParser = new RdfXmlParser();
        //    rdfXmlParser.Load(graph, fileName);

        //    IEnumerable<Triple> triplesToAdd = graph.GetTriples(new EverySelector());

        //    this.VirtuosoManager.UpdateGraph(graphName, new List<Triple>(triplesToAdd), new List<Triple>());
        //}

        //public void UploadTriplesToGraph(List<Triple> triples, string graphn)
        //{
        //    //List<VDS.RDF.Triple> rdfTriples = new List<Triple>();
        //    //foreach (CoreBase.Triple triple in triples)
        //    //{
        //    //    UriNode subject =
        //    //        graph.CreateUriNode(new Uri(triple.Subject));
        //    //    UriNode predicate =
        //    //        graph.CreateUriNode(
        //    //            new Uri(triple.Predicate));
        //    //    LiteralNode @object = graph.CreateLiteralNode(triple.Object);

        //    //    VDS.RDF.Triple rdfTriple = new VDS.RDF.Triple(subject, predicate, @object);
        //    //    rdfTriples.Add(rdfTriple);
        //    //}

        //    this.VirtuosoManager.UpdateGraph(graphn, triples, new List<Triple>());
        //}

        //public void ClearGraph(string graph)
        //{
        //    this.VirtuosoManager.Open(false);
        //    this.VirtuosoManager.ExecuteNonQuery(@"sparql clear graph <" + graph + ">");
        //    this.VirtuosoManager.Close(false);
        //}

        //public List<Uri> GetRootClasses(string graph)
        //{
        //    string rootClassesQuery = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                              @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                              @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                              @"SELECT DISTINCT ?root" + "\n" +
        //                              @"FROM <" + graph + "> " + "\n" +
        //                              @"WHERE" + "\n" +
        //                              @"{" + "\n" +
        //                              @" {" + "\n" +
        //                              @"  ?root a owl:Class" + "\n" +
        //                              @" }" + "\n" +
        //                              @"OPTIONAL" + "\n" +
        //                              @" {" + "\n" +
        //                              @"  ?root rdfs:subClassOf ?rootParent" + "\n" +
        //                              @" }" + "\n" +
        //                              @"FILTER (!bound(?rootParent))" + "\n" +
        //                              @"}";

        //    return QueryHelper.ExecuteListOfUriQuery(this.VirtuosoManager, rootClassesQuery, false);
        //}

        //public List<Uri> GetSubClasses(string graph, string className, bool direct)
        //{
        //    string subClassesQuery = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                             @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                             @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                             @"SELECT DISTINCT ?subClass" + "\n" +
        //                             @"FROM <" + graph + "> " + "\n" +
        //                             @"WHERE {?subClass rdfs:subClassOf <" + className + "> }";

        //    return QueryHelper.ExecuteListOfUriQuery(this.VirtuosoManager, subClassesQuery, direct);
        //}

        //public List<Uri> GetIndividualTypes(string graph, string individualName, bool direct)
        //{
        //    string getTypesQuery = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                           @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                           @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                           @"SELECT DISTINCT ?type" + "\n" +
        //                           @"FROM <http://kms.cctpu.edu.ru/ontologies/2009/5/TPU.owl#> " + "\n" +
        //                           @"WHERE" + "\n" +
        //                           @"{" + "\n" +
        //                           @"<" + individualName + @"> a ?type" + "\n" +
        //                           @"}";

        //    return QueryHelper.ExecuteListOfUriQuery(this.VirtuosoManager, getTypesQuery, direct);
        //}

        //public List<Uri> GetIndividuals(string graph, string className)
        //{
        //    string getIndividualsQuery = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                                 @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                                 @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                                 @"SELECT DISTINCT ?individual" + "\n" +
        //                                 @"FROM <" + graph + ">\n" +
        //                                 @"WHERE" + "\n" +
        //                                 @"{" + "\n" +
        //                                 @"?individual a <" + className + ">\n" +
        //                                 @"}";

        //    return QueryHelper.ExecuteListOfUriQuery(this.VirtuosoManager, getIndividualsQuery, false);
        //}

        //public Dictionary<Uri, List<Uri>> GetObjectPropertiesWithValues(string graph, string individualName)
        //{
        //    string getObjectPropertiesQuery = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                                      @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                                      @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                                      @"SELECT DISTINCT ?objectProperty ?value" + "\n" +
        //                                      @"FROM <" + graph + "> " + "\n" +
        //                                      @"WHERE" + "\n" +
        //                                      @"{" + "\n" +
        //                                      @"<" + individualName + "> ?objectProperty ?value." + "\n" +
        //                                      @" ?objectProperty a owl:ObjectProperty" + "\n" +
        //                                      @"}";

        //    return QueryHelper.ExecuteUriDictionaryOfListOfUriQuery(this.VirtuosoManager, getObjectPropertiesQuery, false);
        //}

        //public List<Uri> GetObjectPropertyValues(string graph, string individualName, string propertyName)
        //{
        //    string getObjectPropertyValues = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                                     @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                                     @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                                     @"SELECT DISTINCT ?value" + "\n" +
        //                                     @"FROM <" + graph + "> " + "\n" +
        //                                     @"WHERE" + "\n" +
        //                                     @"{" + "\n" +
        //                                     @" <" + individualName + "> <" + propertyName + "> ?value." + "\n" +
        //                                     @"}";

        //    return QueryHelper.ExecuteListOfUriQuery(this.VirtuosoManager, getObjectPropertyValues, false);
        //}

        //public Dictionary<Uri, List<LiteralNode>> GetDataPropertiesWithValues(string graph, string individualName)
        //{
        //    string getDataPropertiesWithValues = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                                      @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                                      @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                                      @"SELECT DISTINCT ?dataProperty ?value" + "\n" +
        //                                      @"FROM <" + graph + "> " + "\n" +
        //                                      @"WHERE" + "\n" +
        //                                      @"{" + "\n" +
        //                                      @"<" + individualName + "> ?dataProperty ?value." + "\n" +
        //                                      @" ?dataProperty a owl:DatatypeProperty" + "\n" +
        //                                      @"}";

        //    return QueryHelper.ExecuteUriDictionaryOfListOfLiteralQuery(this.VirtuosoManager, getDataPropertiesWithValues, false);
        //}

        //public List<LiteralNode> GetDataPropertyValues(string graph, string individualName, string propertyName)
        //{
        //    string getDataPropertyValues = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                                     @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                                     @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                                     @"SELECT DISTINCT ?value" + "\n" +
        //                                     @"FROM <" + graph + "> " + "\n" +
        //                                     @"WHERE" + "\n" +
        //                                     @"{" + "\n" +
        //                                     @" <" + individualName + "> <" + propertyName + "> ?value." + "\n" +
        //                                     @"}";

        //    return QueryHelper.ExecuteListOfLiteralQuery(this.VirtuosoManager, getDataPropertyValues, false);
        //}

        public Dictionary<Property, List<string>> GetPropertiesWithLabelsAndValues(string graph, string individualName, string language)
        {
            string getDataPropertyValues = "prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
                                 "prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
                                 "prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
                                 "SELECT DISTINCT ?property ?label ?value" + "\n" +
                                 "FROM <" + graph + "> " + "\n" +
                                 "WHERE" + "\n" +
                                 "{" + "\n" +
                                 " <" + individualName + "> ?property ?value." + "\n" +
                                 " ?property rdfs:label ?label" +
                                 " FILTER (lang(?label) = \"" + language + "\"" +
                                 "}";

            return QueryHelper.ExecutePropertyDictionaryOfListOfStringQuery(this.VirtuosoManager, getDataPropertyValues, null);
        }

        //public Dictionary<Uri, List<LiteralNode>> GetAnnotationValuesForAllEntities(string graph, string annotationName)
        //{
        //    string getAnnotationValuesForAllEntities = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                         @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                         @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                         @"SELECT DISTINCT ?entity value?" + "\n" +
        //                         @"FROM <" + graph + "> " + "\n" +
        //                         @"WHERE" + "\n" +
        //                         @"{" + "\n" +
        //                         @" ?entity <" + annotationName + "> ?value." + "\n" +
        //                         @"}";

        //    return QueryHelper.ExecuteUriDictionaryOfListOfLiteralQuery(this.VirtuosoManager, getAnnotationValuesForAllEntities, true);
        //}

        //public List<LiteralNode> GetAnnotationValuesForEntity(string graph, string entityName, string annotationName)
        //{
        //    string getAnnotationValuesForEntity = @"prefix rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>" + "\n" +
        //                         @"prefix owl:    <http://www.w3.org/2002/07/owl#>" + "\n" +
        //                         @"prefix  rdfs:     <http://www.w3.org/2000/01/rdf-schema#>" + "\n" +
        //                         @"SELECT DISTINCT ?value" + "\n" +
        //                         @"FROM <" + graph + "> " + "\n" +
        //                         @"WHERE" + "\n" +
        //                         @"{" + "\n" +
        //                         @" <" + entityName + "> <" + annotationName + "> ?value." + "\n" +
        //                         @"}";

        //    return QueryHelper.ExecuteListOfLiteralQuery(this.VirtuosoManager, getAnnotationValuesForEntity, true);
        //}
    }
}
